package com.egao.common.module.cooperative.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.Constants;
import com.egao.common.core.exception.BusinessException;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.cooperative.constants.CooperativeProjectConstants;
import com.egao.common.module.cooperative.entity.CooperativeProjectBanner;
import com.egao.common.module.cooperative.mapper.CooperativeProjectBannerMapper;
import com.egao.common.module.cooperative.service.CooperativeProjectBannerService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 产教融合banner 服务接口实现
 *
 * @author root
 */
@Service
public class CooperativeProjectBannerServiceImpl extends ServiceImpl<CooperativeProjectBannerMapper, CooperativeProjectBanner>
        implements CooperativeProjectBannerService {


    @Override
    public List<CooperativeProjectBanner> listPage(PageParam<CooperativeProjectBanner> page) {
        return baseMapper.listPage(page);
    }

    @Override
    public JsonResult saveCooperativeProjectBanner(CooperativeProjectBanner cooperativeProjectBanner) {
        if (cooperativeProjectBanner == null) {
            throw new BusinessException("参数异常");
        }

        if (baseMapper.insert(cooperativeProjectBanner) > 0) {
            return JsonResult.ok("添加成功");
        }

        return JsonResult.error("添加失败");
    }

    @Override
    public JsonResult updateCooperativeProjectBanner(CooperativeProjectBanner cooperativeProjectBanner) {

        if (updateById(cooperativeProjectBanner)) {
            return JsonResult.ok("修改成功");
        }

        return JsonResult.error("修改失败");
    }

    @Override
    public JsonResult deleteCooperativeProjectBanner(CooperativeProjectBanner cooperativeProjectBanner) {
        if (cooperativeProjectBanner == null) {
            throw new BusinessException("参数异常");
        }

        if (cooperativeProjectBanner.getId() == null) {
            throw new BusinessException("主键异常");
        }

        UpdateWrapper<CooperativeProjectBanner> uw = new UpdateWrapper();
        uw.set("deleted", Constants.DELETED_FALSE);
        uw.lambda().eq(CooperativeProjectBanner::getId, cooperativeProjectBanner.getId());
        if (update(uw)) {
            return JsonResult.ok("删除成功");
        }

        return JsonResult.error("删除失败");
    }

    @Override
    public List<CooperativeProjectBanner> listPublicity() {
        return baseMapper.selectList(new QueryWrapper<CooperativeProjectBanner>()
                .select("id, title, img, url")
                .eq("state", CooperativeProjectConstants.BANNER_STATE_PUBLIC)
                .orderBy(true, false, "sort IS NULL, sort ASC, create_time")
        );
    }
}
